package com.avic.gateway;

import com.avic.gateway.common.annotation.ImportFilters;
import com.avic.gateway.filter.FailedRequestPostFilter;
import com.avic.gateway.filter.GatewayErrorFilter;
import com.avic.gateway.filter.LogOutRemoveTokenPostFilter;
import com.avic.gateway.filter.LoginCreateTokenPostFilter;
import com.avic.gateway.filter.ResponseBodyPostFilter;
import com.avic.gateway.filter.TokenValidatePreFilter;
import com.avic.gateway.filter.UriRulePreFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 网关入口
 *
 * @author: huangsheng
 * @date: 2018/8/21 下午1:25
 */
@EnableZuulProxy
@EnableSwagger2
@SpringBootApplication
@EnableEurekaClient
@ImportFilters(filters = {
        GatewayErrorFilter.class,
        LoginCreateTokenPostFilter.class,
        UriRulePreFilter.class,
        FailedRequestPostFilter.class,
        TokenValidatePreFilter.class,
        ResponseBodyPostFilter.class,
        LogOutRemoveTokenPostFilter.class
})
@Configuration
public class GatewayServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServiceApplication.class, args);
    }


    /**
     * 过滤器参数
     **/
    private static final boolean ALLOW_CREDENTIALS = true;
    private static final String ALLOW_ORIGIN = "*";
    private static final String ALLOW_HEADER = "*";
    private static final String ALLOW_METHOD_OPTIONS = "OPTIONS";
    private static final String ALLOW_METHOD_HEAD = "HEAD";
    private static final String ALLOW_METHOD_GET = "GET";
    private static final String ALLOW_METHOD_PUT = "PUT";
    private static final String ALLOW_METHOD_POST = "POST";
    private static final String ALLOW_METHOD_DELETE = "DELETE";
    private static final String ALLOW_METHOD_PATCH = "PATCH";
    private static final String REGISTER_PATH = "/**";

    /**
     * 解决跨域过滤器
     *
     * @return CorsFilter
     */
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(ALLOW_CREDENTIALS);
        config.addAllowedOrigin(ALLOW_ORIGIN);
        config.addAllowedHeader(ALLOW_HEADER);
        config.addAllowedMethod(ALLOW_METHOD_OPTIONS);
        config.addAllowedMethod(ALLOW_METHOD_HEAD);
        config.addAllowedMethod(ALLOW_METHOD_GET);
        config.addAllowedMethod(ALLOW_METHOD_PUT);
        config.addAllowedMethod(ALLOW_METHOD_POST);
        config.addAllowedMethod(ALLOW_METHOD_DELETE);
        config.addAllowedMethod(ALLOW_METHOD_PATCH);
        source.registerCorsConfiguration(REGISTER_PATH, config);
        return new CorsFilter(source);
    }
}
